<%@ page import="java.util.List" %>
<%@ page import="Test_2019_3_4.util.JdbcUtil" %>
<%@ page import="Test_2019_3_4.pojo.Dvd" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018\10\12 0012
  Time: 9:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理数据</title>
</head>
<body>
<div style='position:fixed; z-index:999; top:0;'><h4>欢迎您<%=session.getAttribute("loginName")%>，想走就<a href="login.jsp"
                                                                                                     style="color: red">退出</a>
</h4></div>
<div align='center'>
    <form action='./display.jsp' method='post'>
        <h4>搜关键字：<input type='text' title="序号书名库存" name='search' style="width: 150px; height: 30px"/>
            <input type='submit' value='搜索' style="width: 50px; height: 30px"/>
        </h4>
    </form>
</div>
<form action='./delete.do' method='post' onsubmit="return GL()">
    <table border="1" width="850" align="center">
        <tr>
            <th width=50px>选择</th>
            <th width=60px>序号</th>
            <th width=300px>名称</th>
            <th width=240px>出品日期</th>
            <th width=200px>出品公司</th>
        </tr>
        <%
            //post解决中文乱码
            request.setCharacterEncoding("utf-8");
            //遍历结果集
            JdbcUtil jdbcUtilDvd = new JdbcUtil();
            List<Dvd> dvds;
            //数据总数
            //查询的总条数num和页数pages和页面选择page1
            int num = 0, pages, page1;
            // 判断是否模糊查询
            if (request.getParameter("search") == null) {
                num = jdbcUtilDvd.findCount();
            } else {
                //遍历结果集
                num = jdbcUtilDvd.findCountPage(request.getParameter("search"));
            }
            // 对页数进行操作
            if (num % Dvd.PAGE_SIZE == 0) {
                pages = num / Dvd.PAGE_SIZE;
            } else {
                pages = num / Dvd.PAGE_SIZE + 1;
            }
            // 所需跳转的页数
            if (request.getAttribute("page") == null) {
                page1 = 1;
                if (request.getParameter("search") == null) {
                    dvds = jdbcUtilDvd.find(1);
                } else {
                    dvds = jdbcUtilDvd.findPage(1, request.getParameter("search"));
                }
            } else {
                page1 = (Integer) request.getAttribute("page");
                if (request.getParameter("search") == null) {
                    dvds = jdbcUtilDvd.find(page1);
                } else {
                    dvds = jdbcUtilDvd.findPage(page1, request.getParameter("search"));
                }
            }
            for (Dvd dvd : dvds) {
        %>
        <tr>
            <!-- 输出结果集 -->
            <td align="center">
                <input type='checkbox' name='check' title='choice' value='<%= dvd.getNo() %>'/>
            </td>
            <td align="center">
                <%= dvd.getNo() %>
            </td>
            <td align="center">
                <%= dvd.getName() %>
            </td>
            <td align="center">
                <%= dvd.getTime() %>
            </td>
            <td align="center">
                <%= dvd.getAuthor() %>
            </td>
        </tr>
        <%
            }
        %>
        <tr>
            <td colspan='1' align="center"><input type="submit" value="批量删除" style="text-align: left;"></td>
            <td colspan='4' align="right">
                <a href='./login.do?page=1'>首页</a>
                <%
                    if (page1 == 1) {
                %>
                <a href='./login.do?page=1'>上一页</a>
                <%
                } else {
                %>
                <a href='./login.do?page=<%= page1-1%>'>上一页</a>
                <%
                    }
                %>
                <%
                    for (int i = 1; i < pages + 1; i++) {
                %>
                <a href='./login.do?page=<%= i %>'><%= i %>
                </a>
                <%
                    }
                %>
                <%
                    if (page1 == pages) {
                %>
                <a href='./login.do?page=<%= pages%>'>下一页</a>
                <%
                } else {
                %>
                <a href='./login.do?page=<%= page1+1%>'>下一页</a>
                <%
                    }
                %>
                <a href='./login.do?page=<%= pages%>'>尾页</a>
            </td>
        </tr>
        <script type="text/javascript">
            if (<%= request.getAttribute("MSG")!=null %>) {
                alert('<%=request.getAttribute("MSG") %>');
            }

            /**
             * @return {boolean}
             */
            function GL() {
                if (<%= session.getAttribute("admin") == null %>) {
                    window.alert("很抱歉，您不是管理员！");
                    return false;
                } else {
                    var theResponse = window.prompt("想删库请输入密码：", "请在此输入删库密码");
                    // var truthBeTold = window.confirm("单击“确定”继续。单击“取消”停止。");
                    if (theResponse == 123) {
                        window.alert("又删我库,很难受！");
                        return true;
                    } else {
                        window.alert("小兄弟，库不能随便删的啊！");
                        return false;
                    }
                }
            }
        </script>
    </table>
</form>
</body>
</html>
